﻿using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Wasion.HHU.Data.Models;
using Wasion.HHU.Data.Repositories;
using SQLite.Net;
using System;
using System.Linq.Expressions;

namespace Wasion.HHU.Data.Repositories
{
    public class IcAttrMthDbRepository : BaseDbRepository
    {
        public IcAttrMthDbRepository() : base()
        {

        }
        public IcAttrMthDbRepository(SQLiteConnection connection)
            : base(connection)
        {
        }

        public Task<List<IcAttrMth>> GetAllList()
        {
            List<IcAttrMth> icAttrMths;

            lock (databaseLock)
            {
                icAttrMths = DbConnection.Table<IcAttrMth>().
                    OrderBy(x => x.ClassID).ThenBy(x=>x.PID).ThenBy(x => x.AttrMth).ThenBy(x => x.seq).
                    ToList();
            }
            return Task.FromResult(icAttrMths);
        }

        public Task<List<IcAttrMth>> GetList(Expression<Func<IcAttrMth, bool>> filter)
        {
            List<IcAttrMth> icAttrMths;

            lock (databaseLock)
            {
                icAttrMths = DbConnection.Table<IcAttrMth>().
                    OrderByDescending(x => x.ClassID).
                    Where(filter).
                    ToList();
            }
            return Task.FromResult(icAttrMths);
        }

        public void Insert(IcAttrMth icAttrMth)
        {
            lock (databaseLock)
            {
                DbConnection.CreateTable<IcAttrMth>();
                DbConnection.Insert(icAttrMth);
            }
        }

        public void Update(IcAttrMth icAttrMth)
        {
            lock (databaseLock)
            {
                DbConnection.Update(icAttrMth);
            }
        }

        public IcAttrMth Find(object primaryKey)
        {
            IcAttrMth icAttrMth;

            lock (databaseLock)
            {
                icAttrMth = DbConnection.Find<IcAttrMth>(primaryKey);
            }
            return icAttrMth;
        }

        public void Delete(object primaryKey)
        {
            lock (databaseLock)
            {
                DbConnection.Delete<IcAttrMth>(primaryKey);
            }
        }
    }
}
